package com.qf.controller.popularize;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGrouponRulesService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller.popularize
 * @Description:
 * @Date 2022/10/21 20:56
 */
@RestController
@RequestMapping("/admin/groupon")
public class AdminGrouponRulesController {

    @Autowired
    DtsGrouponRulesService grouponRulesService;

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page ,
                       @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc") String order,
                       Integer goodsId){

        Map<String,Object> data = grouponRulesService.selectlistuserByPage(page,limit,sort,order,goodsId);

        return ResponseUtil.ok(data);
    }


    @PostMapping("/create")
    public Object create(@RequestBody Map<String,Object> data) throws Exception {
        Object result = checkMap(data);
        if (result!=null){
            return result;
        }
        Integer member = Integer.parseInt((String) data.get("discountMember"));
        DtsGrouponRules grouponRules = new DtsGrouponRules();
        long goodsId = Integer.parseInt((String) data.get("goodsId"));
        grouponRules.setGoodsId(goodsId);
        BigDecimal discount = new BigDecimal((String) data.get("discount"));
        grouponRules.setDiscount(discount);
        grouponRules.setDiscountMember(member);
        String time = String.valueOf(data.get("expireTime"));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1= formatter.parse(time);
        grouponRules.setExpireTime(d1);
        grouponRules.setAddTime(new Date());
        grouponRules.setDeleted(false);
        grouponRules.setUpdateTime(new Date());
//        System.out.println(coupon.toString());
        grouponRulesService.insert(grouponRules);

        return ResponseUtil.ok(grouponRules);
    }

    private Object checkMap( Map<String,Object> data) {
        Integer goodsId = Integer.parseInt((String) data.get("goodsId"));
        Integer discount = Integer.parseInt((String) data.get("discount"));
        Integer discountMember = Integer.parseInt((String) data.get("discountMember"));
        if (goodsId==null || discount == null || discountMember == null){
            return ResponseUtil.fail(-1,"商品id、团购人数和折扣不能为空");
        }
        return null;
    }


    @PostMapping("/update")
    public Object update(@RequestBody DtsGrouponRules grouponRules){
        grouponRules.setUpdateTime(new Date());
        grouponRulesService.update(grouponRules);
        return ResponseUtil.ok(grouponRules);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGrouponRules grouponRules){
        grouponRulesService.delete(grouponRules);
        return ResponseUtil.ok();
    }
}
